package com.yjhb.android.sbz.mvp.settinginfo;

import com.google.gson.JsonObject;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.entity.UserInfo;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

/**
 * 创建者 zx
 * 创建时间 2020/11/23 17:51
 */
public class SettingInfoPresenter extends ConventionPresenter<SettingInfoView> {
    private final String TAG = "SettingInfoPresenter";

    public SettingInfoPresenter(SettingInfoView settingInfoView) {
        super(settingInfoView);
    }

    @Override
    protected void onDestroy() {

    }

    public void getUserInfo() {
        if (checkUnLogin()) return;
        HttpTool.request(model.getUserInfo(App.userInfo.getId()), new HttpObserver<UserInfo>() {
            @Override
            public void onResult(UserInfo data) {
                if (view != null) {
                    view.onUserInfo(data);
                }
            }

            @Override
            public void onError(int code, String msg) {

            }
        });
    }


    /**
     * 更新头像
     *
     * @param avatar
     */
    private void updateHead(String avatar) {
        if (checkUnLogin()) return;
        HttpTool.request(model.updateHead(App.userInfo.getId(), avatar), new HttpObserver<String>() {
            @Override
            public void onResult(String data) {
                if (view != null) {
                    view.onOk();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 更新昵称
     *
     * @param nickName
     */
    public void updateNickName(String nickName) {
        if (checkUnLogin()) return;
        HttpTool.request(model.updateNickName(App.userInfo.getId(), nickName), new HttpObserver<String>() {
            @Override
            public void onResult(String data) {
                if (view != null) {
                    view.onOk();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 认证
     *
     * @param name
     * @param idCard
     */
    public void updateIdentityInfo(String name, String idCard) {
        if (checkUnLogin()) return;
        HttpTool.request(model.updateIdentityInfo(App.userInfo.getId(), name, idCard), new HttpObserver<String>() {
            @Override
            public void onResult(String data) {
                if (view != null) {
                    view.onOk();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 认证
     *
     * @param name
     * @param zfbAccount
     */
    public void updateZfbAccount( String name,String zfbAccount,String idCard) {
        if (checkUnLogin()) return;
        HttpTool.request(model.updateZfbAccount(App.userInfo.getId(), zfbAccount, name,idCard), new HttpObserver<String>() {
            @Override
            public void onResult(String data) {
                if (view != null) {
                    view.onOk();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    @Override
    public void uploadResult(String result) {
        //更新头像信息
        updateHead(result);
    }

    public void bindWx(String code) {
        if (checkUnLogin()) return;
        HttpTool.request(model.bindingWx(App.userInfo.getId(), code), new HttpObserver<JsonObject>() {
            @Override
            public void onResult(JsonObject data) {
                if (view != null) {
                    view.onOk();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 更改支付宝账号
     *
     * @param account
     */
    public void changeZfb(String account) {
        if(view!=null){
            view.onWait("正在加载...");
        }
        HttpTool.request(model.changeZfbAccount(account), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onZfbChangeSuccess();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(-3, msg);
                }
            }
        });
    }

}
